/**
 * 
 */
package com.wilmar.process.entity;

import java.io.Serializable;

import org.jbpm.api.identity.Group;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.exception.DirtyDataException;

/**
 * 工作流子系统采用的角色对象
 * 
 * @author bobo E-mail:zholibo_java@msn.cn
 * @version 1.0.0 at Jul 29, 2009 5:34:33 PM
 * 
 */
public class Role extends ProcessSystemResourceEntity implements Serializable,
		Group {

	/**
	 * 由字符串OuterSystem-Role-RoleCode构造Role对象，字符串即是标示符
	 * 
	 * @param info
	 *            信息字符串
	 * @throws DirtyDataException
	 *             字符串结构出错
	 */
	public Role(String info) throws DirtyDataException {
		super(info);
		if (info == null)
			throw new DirtyDataException("The String is null!");
		String[] strArray = info
				.split(ProcessSystemDeclareKeys.PROCESS_SYSTEM_USER_OUTERSYSTEM_SEPTATOR);
		if (strArray == null
				|| strArray.length < 3
				|| !ProcessSystemDeclareKeys.PROCESS_SYSTEM_PROCESS_ASSIGN_MARK_ROLE
						.equalsIgnoreCase(strArray[1]))
			throw new DirtyDataException("The String is a dirty data:" + info);
		else {
			this.outerSystem = strArray[0];
			String code = "";
			for (int index = 2; index < strArray.length; index++) {
				code += strArray[index];
				if (index != strArray.length - 1) {
					code += ProcessSystemDeclareKeys.PROCESS_SYSTEM_USER_OUTERSYSTEM_SEPTATOR;
				}
			}
			this.roleCode = code;
		}
	}

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 3711020604579419893L;

	private String roleCode;

	private String roleName;

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (outerSystem == null) {
			if (other.outerSystem != null)
				return false;
		} else if (!outerSystem.equals(other.outerSystem))
			return false;
		if (roleCode == null) {
			if (other.roleCode != null)
				return false;
		} else if (!roleCode.equals(other.roleCode))
			return false;
		return true;
	}

	public String getId() {
		return this.outerSystem
				+ ProcessSystemDeclareKeys.PROCESS_SYSTEM_USER_OUTERSYSTEM_SEPTATOR
				+ ProcessSystemDeclareKeys.PROCESS_SYSTEM_PROCESS_ASSIGN_MARK_ROLE
				+ ProcessSystemDeclareKeys.PROCESS_SYSTEM_USER_OUTERSYSTEM_SEPTATOR
				+ this.roleCode;
	}

	/**
	 * @return the roleCode
	 */
	public String getRoleCode() {
		return roleCode;
	}

	/**
	 * @return the roleName
	 */
	public String getRoleName() {
		return roleName;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((outerSystem == null) ? 0 : outerSystem.hashCode());
		result = prime * result
				+ ((roleCode == null) ? 0 : roleCode.hashCode());
		return result;
	}

	/**
	 * @param roleCode
	 */
	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}

	/**
	 * @param roleName
	 */
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	/**
	 * @see org.jbpm.api.identity.Group#getName()
	 */
	public String getName() {
		return this.getId();
	}

	/**
	 * @see org.jbpm.api.identity.Group#getType()
	 */
	public String getType() {
		return ProcessSystemDeclareKeys.PROCESS_SYSTEM_PROCESS_ASSIGN_MARK_ROLE;
	}

}
